package com.tansun.easycare.rule.utils;

import java.util.HashMap;
import java.util.Map;

import com.tansun.easycare.core.config.Global;
import com.tansun.easycare.rule.exception.BizException;

public class HbaseUtils {

    private static String partnerId = "onebank";

    private static String appKey = "bdid";


    public static String selectByRowKey(String key) throws BizException {
        Map<String, Object> request = new HashMap<String, Object>();
        request.put("partnerId", partnerId);
        request.put("appKey", appKey);
        request.put("version", "3.0");
        request.put("platformSerialNo", key);
        request.put("eventType", "dec_select_hbase_0001");
        request.put("occurTime", System.currentTimeMillis());
        Map<String, Object> txParameterMap = new HashMap<String, Object>();
        txParameterMap.put("key", key);
        request.put("txParameter", txParameterMap);
        try {
            Map<String, Object> resultMap = HttpUtils.post(request, Global.getConfig("Hbase.Http.select"));
            String respCode = (String) resultMap.get("respCode");
            if ("9000".equals(respCode)) {
                @SuppressWarnings("unchecked")
                Map<String, Object> reData = (Map<String, Object>) resultMap.get("resultData");

                String message = (String) reData.get("message");
                return message;
                //resultData
            } else {
                throw new BizException(respCode, (String) resultMap.get("respDesc"));
            }

        } catch (Exception e) {
            throw new BizException("", e.getMessage());

        }

    }

    public static void save(String message, String key) throws BizException {
        Map<String, Object> request = new HashMap<String, Object>();
        request.put("partnerId", partnerId);
        request.put("appKey", appKey);
        request.put("version", "3.0");
        request.put("eventType", "dec_insert_hbase_0001");
        request.put("platformSerialNo", key);
        request.put("occurTime", System.currentTimeMillis());
        Map<String, Object> txParameterMap = new HashMap<String, Object>();
        txParameterMap.put("rowkey", key);
        txParameterMap.put("message", message);
        request.put("txParameter", txParameterMap);
        try {
            Map<String, Object> resultMap = HttpUtils.post(request, Global.getConfig("Hbase.Http.save"));
            if (resultMap.get("respCode").equals("9000")) {

            } else {
                throw new BizException((String) resultMap.get("respCode"), (String) resultMap.get("respDesc"));
            }

        } catch (Exception e) {
            throw new BizException("", e.getMessage());
        }
    }

    public static void main(String[] args) {
        HbaseUtils.save("保存信息", "234567890");
        HbaseUtils.selectByRowKey("234567890");
    }

}
